package cn.accjiyun.inventory.dao.impl;

import cn.accjiyun.inventory.dao.GoodsDAO;
import cn.accjiyun.inventory.model.Goods;
import cn.accjiyun.inventory.until.DebugLog;
import cn.accjiyun.inventory.until.HibernateUtil;
import cn.accjiyun.inventory.until.SessionUtil;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Restrictions;

import java.beans.Expression;
import java.util.List;

/**
 * Created by jiyun on 2016/6/30.
 */
public class GoodsDAOimpl implements GoodsDAO{

    SessionUtil sessionUtil = new SessionUtil();
    /**
     * Get all goods
     * @return List<Goods>
     */
    @Override
    public List<Goods> selectAll() {
        return sessionUtil.selectAll(Goods.class);
    }

    /**
     * delete goods
     * @param goods
     */
    @Override
    public boolean delete(Goods goods) {
        if (sessionUtil.delete(goods)) return true;
        else return false;
    }

    /**
     * add goods to mysql
     * @param goods
     * @return true if add goods successful
     */
    @Override
    public boolean add(Goods goods) {
        if (sessionUtil.add(goods)) return true;
        else return false;
    }

    /**
     * update goods
     * @param goods
     * @return true if update goods successful
     */
    @Override
    public boolean update(Goods goods) {
        if (sessionUtil.update(goods)) return true;
        return false;
    }

    /**
     * select goods by id
     * @param id
     * @return Goods if select successful
     */
    @Override
    public List<Goods> selectById(String id) {
        return sessionUtil.selectById(id, Goods.class);
    }

    /**
     * select goods by name
     * @param name
     * @return List<Goods> if select successful
     */
    @Override
    public List<Goods> selectByName(String name) {
        Session session = HibernateUtil.getSession();
        List<Goods> goodsList = session.createCriteria(Goods.class)
                .add(Restrictions.like("name", "%" + name + "%")).list();
        return goodsList;
    }

    /**
     * select goods by type
     * @param type
     * @return List<Goods> if select successful
     */
    @Override
    public List<Goods> selectByType(int type) {
        Session session = HibernateUtil.getSession();
        List<Goods> goodsList = session.createCriteria(Goods.class)
                .add(Restrictions.eq("type", type)).list();
        return goodsList;
    }

}
